package com.ruoyi.energy.service.impl;

import cn.hutool.core.util.IdUtil;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.core.utils.SecurityUtils;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.energy.domain.NyTrans;
import com.ruoyi.energy.domain.NyTransAllowCheck;
import com.ruoyi.energy.domain.NyTransAllowDetail;
import com.ruoyi.energy.domain.NyTransLoad;
import com.ruoyi.energy.domain.vo.NyTransAllowDetailVO;
import com.ruoyi.energy.domain.vo.NyTransAllowDetialVOR;
import com.ruoyi.energy.mapper.NyTransAllowCheckMapper;
import com.ruoyi.energy.mapper.NyTransAllowMapper;
import com.ruoyi.energy.service.INyTransAllowCheckService;
import com.ruoyi.energy.service.INyTransLoadService;
import com.ruoyi.energy.service.INyTransService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 变压器监测Service业务层处理
 *
 * @author cjie
 * @date 2022-03-07
 */
@Service
public class NyTransAllowCheckServiceImpl implements INyTransAllowCheckService {
    @Autowired
    private NyTransAllowCheckMapper nyTransAllowCheckMapper;

    @Autowired
    private NyTransAllowMapper nyTransAllowMapper;

    @Autowired
    private INyTransLoadService nyTransLoadService;

    @Autowired
    private INyTransService nyTransService;

    /**
     * 查询变压器监测
     *
     * @param id 变压器监测ID
     * @return 变压器监测
     */
    @Override
    public NyTransAllowCheck selectNyTransAllowCheckById(String id) {
        return nyTransAllowCheckMapper.selectNyTransAllowCheckById(id);
    }

    /**
     * 查询变压器监测列表
     *
     * @param nyTransAllowCheck 变压器监测
     * @return 变压器监测
     */
    @Override
    public List<NyTransAllowCheck> selectNyTransAllowCheckList(NyTransAllowCheck nyTransAllowCheck) {
        return nyTransAllowCheckMapper.selectNyTransAllowCheckList(nyTransAllowCheck);
    }

    /**
     * 新增变压器监测
     *
     * @param nyTransAllowCheck 变压器监测
     * @return 结果
     */
    @Override
    public int insertNyTransAllowCheck(NyTransAllowCheck nyTransAllowCheck) {
        nyTransAllowCheck.setId(IdUtil.getSnowflake(0, 0).nextIdStr());
        nyTransAllowCheck.setCreateTime(DateUtils.getNowDate());
        nyTransAllowCheck.setCreateBy(SecurityUtils.getUsername());
        // 添加 箱变名称、线径、申报容量、企业名称。
        String detailId = nyTransAllowCheck.getDetailId();
        NyTransAllowDetailVO detail = nyTransAllowMapper.selectNyTransAllowDetailByDetailId(detailId);
        nyTransAllowCheck.setTransName(detail.getTransName());
        nyTransAllowCheck.setDiameter(detail.getDiameter());
        nyTransAllowCheck.setAllow(detail.getAllow());
        nyTransAllowCheck.setEnName(detail.getEnName());
        return nyTransAllowCheckMapper.insertNyTransAllowCheck(nyTransAllowCheck);
    }

    /**
     * 修改变压器监测
     *
     * @param nyTransAllowCheck 变压器监测
     * @return 结果
     */
    @Override
    public int updateNyTransAllowCheck(NyTransAllowCheck nyTransAllowCheck) {

        return nyTransAllowCheckMapper.updateNyTransAllowCheck(nyTransAllowCheck);
    }

    /**
     * 批量删除变压器监测
     *
     * @param ids 需要删除的变压器监测ID
     * @return 结果
     */
    @Override
    public int deleteNyTransAllowCheckByIds(String[] ids) {
        return nyTransAllowCheckMapper.deleteNyTransAllowCheckByIds(ids);
    }

    /**
     * 删除变压器监测信息
     *
     * @param id 变压器监测ID
     * @return 结果
     */
    @Override
    public int deleteNyTransAllowCheckById(String id) {
        return nyTransAllowCheckMapper.deleteNyTransAllowCheckById(id);
    }

    static Integer result =0;
    @Override
    public int batchInsertNyTransAllowDetailVO(NyTransAllowDetialVOR vor) {
//        if (StringUtils.isNull(map) || map.size() == 0) {
//            throw new CustomException("批量抄录数据不能为空！");
//        }
        Long value = 0L;
        if(StringUtils.isNotEmpty(vor.getCurLoad())){
            // 更新 箱变台账 当前值，
            Long transId = Long.parseLong(vor.getTransId());
            value = Long.parseLong(vor.getCurLoad());
            NyTrans nyTrans = new NyTrans();
            nyTrans.setId(transId);
            nyTrans.setCurLoad(value);
            nyTransService.updateNyTrans(nyTrans);
            //插入负荷
            NyTransLoad nyTransLoad = new NyTransLoad();
            nyTransLoad.setTransId(transId);
            nyTransLoad.setValue(value);
            nyTransLoad.setCheckTime(DateUtils.getNowDate());
            nyTransLoadService.insertNyTransLoad(nyTransLoad);
        }
        if(StringUtils.isNotEmpty(vor.getCurList()) && value >0){
            //存在更新，不存在插入
            NyTransAllowCheck search = new NyTransAllowCheck();
            NyTransAllowCheck insert = new NyTransAllowCheck();
            NyTransAllowCheck update = new NyTransAllowCheck();
            NyTransAllowDetail allow = new NyTransAllowDetail();
            vor.getCurList().stream().forEach(item->{
                //NyTransAllowCheck insert
//            search.setDetailId(item.getId());
//            search.setCheckTime(item.getCheckTime());
//            List<NyTransAllowCheck> NyTransAllowChecks = nyTransAllowCheckMapper.selectNyTransAllowCheckList(search);
//            if(NyTransAllowChecks != null && NyTransAllowChecks.size()>0){
//                //update
//                update.setId(NyTransAllowChecks.get(0).getId());
//                update.setValue(Long.parseLong(item.getCurValue()));
//                result=this.updateNyTransAllowCheck(update);
//            }else {
//                //insert
//                insert.setValue(Long.parseLong(item.getCurValue()));
//                insert.setDetailId(item.getId().toString());
//                insert.setCheckTime(item.getCheckTime());
//                result = this.insertNyTransAllowCheck(insert);
//            }
                //insert
                if(StringUtils.isNotEmpty(item.getCurValue())){
                    insert.setValue(Long.parseLong(item.getCurValue()));
                    insert.setDetailId(item.getId().toString());
                    insert.setCheckTime(DateUtils.getNowDate());
                    result = this.insertNyTransAllowCheck(insert);
                    allow.setId(item.getId());
                    allow.setCurValue(item.getCurValue());
                    nyTransAllowMapper.updateNyTransAllowDetail(allow);
                }

            });

        }


        return result;
    }
}
